package zhoufu.sz.sanxiao_check.ui.adapter;

import android.databinding.OnRebindCallback;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.List;

import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.ui.adapter.items.DataBindingItem;
import zhoufu.sz.sanxiao_check.ui.adapter.items.ProgressItem;

/**
 * 项目： eu.davidea.samples.flexibleadapter.fragments
 * 时间： 2016/6/22 10:00
 * 人员： hyc
 * 功能： 实现DataBinding的Adapter基类
 */

public abstract class FlexibleDBAdapter<T,ITEM extends DataBindingItem> extends FlexibleAdapter<AbstractFlexibleItem> {

    public static final Object DB_PAYLOAD = new Object();
    private final OnRebindCallback _rebindCallback = new OnRebindCallback() {
        @Override
        public boolean onPreBind(ViewDataBinding binding) {
            if (mRecyclerView == null || mRecyclerView.isComputingLayout()) {
                return true;
            }
            int childAdapterPosition = mRecyclerView.getChildAdapterPosition(binding.getRoot());
            if (childAdapterPosition == RecyclerView.NO_POSITION) {
                return true;
            }
            notifyItemChanged(childAdapterPosition, DB_PAYLOAD);
            return false;
        }
    };

    public FlexibleDBAdapter(@NonNull List<AbstractFlexibleItem> items, T editWithSearchFragment) {
        super(items,editWithSearchFragment);
    }

    public FlexibleDBAdapter(@NonNull List<AbstractFlexibleItem> items) {
        this(items,null);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(parent.getContext());
        }

        switch (viewType) {
            case R.layout.progress_item:
                return new ProgressItem().createViewHolder(this, mInflater, parent);
            default:
                ITEM item=childItem(parent,viewType);
              return  item.createViewHolder(this,mInflater,parent);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
    }

    protected abstract ITEM childItem(ViewGroup parent, int viewType);

}